/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.codeminer.parts.database.sql;


/**
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public final class Select extends Endable {
    private final String[] columnNames;

    /**
     * Constructs a select statement with columnNames or empty when all
     * should be selected
     *
     *
     * @param columnNames
     */
    Select(SQL sql, String[] columnNames) {
        super(sql);
        this.columnNames = columnNames.clone();
    }

    public From from(String tableName) {
        return new From(getSql(), tableName);
    }

    @Override
    String getSQLString() {
        if (columnNames.length > 0) {
            boolean first = true;
            StringBuilder builder = new StringBuilder("SELECT ");
            for (String string : columnNames) {
                if (!first) {
                    builder.append(',');
                }
                builder.append(string);
                first = false;
            }
            return builder.toString();
        } else {
            return "SELECT *";
        }
    }
}
